<?php
/* SVN FILE: $Id: splash.php 220 2009-09-16 05:26:38Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* ibMarket ACP home page
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 220 $
* @lastrevision $Date: 2009-09-16 05:26:38 +0000 (Wed, 16 Sep 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-09-16 05:26:38 +0000 (Wed, 16 Sep 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/modules_admin/splash/splash.php $
*/


if ( ! defined( 'IN_ACP' ) )
{
	print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded 'admin.php'.";
	exit();
}

class admin_ibmarket_splash_splash extends ipsCommand
{
	/**
	 * Skin object
	 *
	 * @access	private
	 * @var		object			Skin templates
	 */
	private $html;
	
	/**
	 * Shortcut for url
	 *
	 * @access	private
	 * @var		string			URL shortcut
	 */
	private $form_code;
	
	/**
	 * Shortcut for url (javascript)
	 *
	 * @access	private
	 * @var		string			JS URL shortcut
	 */
	private $form_code_js;
	
	/**
	 * Main class entry point
	 *
	 * @access	public
	 * @param	object		ipsRegistry reference
	 * @return	void		[Outputs to screen]
	 */
	public function doExecute( ipsRegistry $registry )
	{
		$this->html = $this->registry->output->loadTemplate('cp_skin_splash');
		
		$this->registry->getClass('class_localization')->loadLanguageFile( array( 'admin_market' ) );
		
		$this->form_code	= $this->html->form_code	= 'module=splash&amp;section=splash';
		$this->form_code_js	= $this->html->form_code_js	= 'module=splash&section=splash';
		
		require IPS_ROOT_PATH . "setup/sources/base/setup.php";
		
		$latestVersions = array();
		$this->DB->build(array('select'=>'*','from'=>'upgrade_history','where'=>"upgrade_app='ibmarket'",'order'=>"upgrade_version_id DESC",'limit'=>array(5)));
		$q = $this->DB->execute();
		while($version = $this->DB->fetch($q))
		{
			$latestVersions[] = $version;
		}
		
		require_once( IPS_KERNEL_PATH . 'classXML.php' );
		$xml = new classXML( $this->settings['gb_char_set'] );
		$xml->load( marketRegistry::updates );
		
		$updates = array();
		foreach( $xml->fetchElements( 'entry' ) as $entry )
		{
			$data = $xml->fetchElementsFromRecord( $entry );
			
			$updates[] = $data['title'];
			
			if(count($updates) >= 10)
			{
				break;
			}
		}
		
		$xml->load( marketRegistry::versionXML );
		$latestXMLArray = $xml->fetchXMLAsArray();
		$latest = IPSSetUp::fetchAppVersionNumbers( 'ibmarket' );
		if($this->settings['market_latest'] == 'unstable' || marketRegistry::unstable)
		{
			$latest['available'][0] = $latestXMLArray['version']['unstable']['version_number']['#alltext'];
			$latest['available'][1] = $latestXMLArray['version']['unstable']['version_name']['#alltext'];
			$latest['available'][2] = $latestXMLArray['version']['unstable']['download_url']['#alltext'];
		}
		else
		{
			$latest['available'][0] = $latestXMLArray['version']['stable']['version_number']['#alltext'];
			$latest['available'][1] = $latestXMLArray['version']['stable']['version_name']['#alltext'];
			$latest['available'][2] = $latestXMLArray['version']['stable']['download_url']['#alltext'];
		}
		
		$statistics = array();
		$statistics['total_items'] = count($this->caches['market_items']);
		$statistics['unique_items'] = count(marketRegistry::getItemsByFiles());
		$statistics['plugins'] = count($this->caches['market_plugin']);
		$statistics['wages'] = count($this->caches['market_wages']['id']);
		$statistics['member_wages'] = count($this->caches['market_wages']['member']);
		$statistics['group_wages'] = count($this->caches['market_wages']['group']);
		$member = $this->DB->buildAndFetch(array('select'=>'*','from'=>'members','order'=>$this->settings['market_members_field']." DESC",'limit'=>array(1)));
		$statistics['most_money'] = $member[$this->settings['market_members_field']];
		$statistics['most_money_member'] = marketFormatter::formatName($member);
		$total = $this->DB->buildAndFetch(array('select'=>'SUM('.$this->settings['market_members_field'].') total','from'=>'members'));
		$statistics['money_circulating'] = $total['total'];
		$member = $this->DB->buildAndFetch(array(
													'select'	=> 'COUNT(*) as no_item',
													'from'		=> array('market_inventory'=>'i'),
													'add_join'	=> array(
																			array(
																					'select' => 'm.*',
																					'from' => array('members'=>'m'),
																					'where' => 'm.member_id = i.member_id',
																					'type' => 'inner',
																				)
																		),
													'group'		=> 'i.member_id',
													'order'		=> 'no_item DESC',
													'limit'		=> array(1),
											)	);
		$statistics['most_inventory'] = $member['no_item'];
		$statistics['most_inventory_member'] = marketFormatter::formatName($member);
		$total = $this->DB->buildAndFetch(array('select'=>'COUNT(*) total','from'=>'market_inventory'));
		$statistics['total_inventory'] = $total['total'];
		$total = $this->DB->buildAndFetch(array('select'=>'item_id,item_name,SUM(quanity) as sum_bought','from'=>'market_buy_logs','where'=>"method='buy'",'group'=>'item_id','order'=>'sum_bought DESC','limit'=>array(1)));
		$statistics['most_bought'] = $total['sum_bought'];
		$statistics['most_bought_item'] = is_array($this->caches['market_items'][$temp['item_id']]) ? $this->caches['market_items'][$temp['item_id']]['item_name'] : $temp['item_name'];
		$total = $this->DB->buildAndFetch(array('select'=>'SUM(quanity) as total','from'=>'market_buy_logs','where'=>"method='buy'"));
		$statistics['total_bought'] = $total['total'];
		
		$this->registry->output->html = $this->html->splash_page($latestVersions, $updates, $statistics, array(), $latest);
		
		$this->registry->getClass('output')->addToDocumentHead('importcss',$this->settings['skin_app_url']."splash.css");
		$this->registry->getClass('output')->html_main .= $this->registry->getClass('output')->global_template->global_frame_wrapper();
		$this->registry->getClass('output')->sendOutput();
	}
}